package com.steven.sync.mianshi;

import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 一个线程打印 A-Z 一个线程打印1-26 要交叉打印A1B2C3....Z26
 */
public class Test3_jiaoti_A1B2C3 {

      static AtomicInteger atomicInteger = new AtomicInteger(1);
      static  final  String FLAG ="ok";

    public static void main(String[] args) {

        String array []={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
        Lock lock = new ReentrantLock();
        Condition zimu = lock.newCondition();
        Condition shuzi = lock.newCondition();

        new Thread(()->{
            for (int i = 1; i < 27; i++) {
                System.out.println("第"+i+"个字母");
                try {
                    lock.lock();
                    while (1==atomicInteger.get()){
                        try {
                            shuzi.await();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println(i);
                    atomicInteger.incrementAndGet();
                    zimu.signalAll();
                } finally {
                    lock.unlock();
                }
            }
        }).start();


        //1打字母 0打数字
        new Thread(() -> {
            for (int i = 0; i < array.length; i++) {
                System.out.println("第"+i+"个数字");
                try {
                    lock.lock();
                    while (0 == atomicInteger.get()) {
                        try {
                            zimu.await();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println(array[i]);
                    atomicInteger.decrementAndGet();
                    shuzi.signalAll();
                } catch (Exception e) {
                    e.printStackTrace();
                }finally {
                    lock.unlock();
                }
            }
        }).start();





    }
}
